package B刷题.二叉树;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author wei.zhao
 * @description: 145.二叉树的后序遍历
 * @date 2022年11月09日 16:31
 */
public class Code03_PostorderTraversal {

    public List<Integer> postorderTraversal(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> collectStack = new Stack<>();
        ArrayList<Integer> result = new ArrayList<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            collectStack.add(pop);
            if (pop.left != null) {
                stack.add(pop.left);
            }
            if (pop.right != null) {
                stack.add(pop.right);
            }
        }
        while (!collectStack.isEmpty()) {
            result.add(collectStack.pop().val);
        }
        return result;
    }

}
